// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Constraint/RBAR1Parser.h"

#include "gtest/gtest.h"

#include "DataStructure/Common/Constant.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRBAR1Service.h"

#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;

using namespace SG::DataStructure::FEM;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;
using SG::DataStructure::Common::TOLERANCE;

class RBAR1ParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (RBAR1ParserTest, ParseEntry1)
{
    std::vector<std::string> Entry = { "RBAR1         99       2       3  123456                  " };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRBAR1 (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::RBAR1Data data;

    DB_SERVICEFACTORY->get<IRBAR1Service> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 99);
    ASSERT_TRUE (data.m_masterNode == 2);
    ASSERT_TRUE (data.m_slaveNodes == 3);

    ASSERT_TRUE (data.GetSlaveDOFs ().size () == 6);
    ASSERT_TRUE (data.GetSlaveDOFs ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetSlaveDOFs ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetSlaveDOFs ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Uz);
    ASSERT_TRUE (data.GetSlaveDOFs ()[3] == SG::DataStructure::FEM::DOFDirections::DOF_Rotx);
    ASSERT_TRUE (data.GetSlaveDOFs ()[4] == SG::DataStructure::FEM::DOFDirections::DOF_Roty);
    ASSERT_TRUE (data.GetSlaveDOFs ()[5] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_NEAR (data.m_alpha, 0.0, TOLERANCE);
    ASSERT_NEAR (data.m_refTemperature, 0.0, TOLERANCE);
}

TEST_F (RBAR1ParserTest, SlaveDOFLessThan6)
{
    std::vector<std::string> Entry = { "RBAR1         88       2       3     126        " };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRBAR1 (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::RBAR1Data data;

    DB_SERVICEFACTORY->get<IRBAR1Service> ()->FindById (88, data);

    // ASSERT_TRUE (data.m_id == 88);
    ASSERT_TRUE (data.m_masterNode == 2);
    ASSERT_TRUE (data.m_slaveNodes == 3);

    ASSERT_TRUE (data.GetSlaveDOFs ().size () == 3);
    ASSERT_TRUE (data.GetSlaveDOFs ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetSlaveDOFs ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetSlaveDOFs ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_NEAR (data.m_alpha, 0.0, TOLERANCE);
    ASSERT_NEAR (data.m_refTemperature, 0.0, TOLERANCE);
}
